/**
 * Created by Administrator on 2017/7/7 0007.
 */
"use strict";
var express = require('express'),
    router = express.Router(),
    mongoosekeeper = require('../db/mongokeeper'),
    articleModel = require('../db/mongokeep-use');


var platform,
    app_ame, app_type, img, version_des,username;
router.post('/PostAppModel', function (req, res) {
    var json =
        {
            "code": "1",
            "succeed": "succeed",
            "content": {
                "msg": "密码错误"
            }
        };
    platform = req.param('platform');
    app_ame = req.param('app_ame');
    app_type = req.param('app_type');
    username = req.param('username');

    img = req.param('img');
    version_des = req.param('version_des');
    if (0==platform .length) {
        FailureJson(json, res, "没有传平台参数");
        return;
    } else if (0== app_ame.length) {
        FailureJson(json, res, "微应用名字为空");
        return;
    } else if (0 == img.length) {
        FailureJson(json, res, "微应用logo为空");
        return;
    } else if (0 == version_des.length) {
        FailureJson(json, res, "微应用介绍为空");
        return;
    }

    mongoosekeeper.use(isExit, function (err, docs) {
        if (err) {
            console.log(err);
        } else if (docs.length > 0) {
            FailureJson(json, res, "微应用名字已经存在");
        } else {
            var app = new articleModel({
                app_ame: app_ame,
                app_type: app_type,
                username: username,
                img: img,
                version_des: version_des,
            });
            app.save();
            SucceeJson(json, res, "存入成功");
            //     if (err) {
            //         FailureJson(json, res, err);
            //     } else {
            //         SucceeJson(json, res, "存入成功");
            //     }
            // });
        }
    });


})
;

function isExit(proxy) {
    var search = {'app_ame': app_ame}; //获取所有文章
    var query = articleModel.find(search).limit(10);
    query.sort('-create_date');
    query.exec(proxy);
}

function addApp(proxy) {
    var app = new articleModel({
        app_ame: app_ame,
        app_type: app_type,
        img: img,
        version_des: version_des,
    });
    var add = app.save();
    add.exec(proxy);
}
var app_list_json =
    {
        "code": "1",
        "succeed": "succeed",
        "content": {
            "msg": []
        }
    };
router.get('/LoadAllApp', function (req, res) {
    username = req.param('username');
    app_ame = req.param('app_ame');
    mongoosekeeper.use(queryArticle, function (err, posts) {
        if (err) {
            throw err;
        }
        else {
            app_list_json.code = "1";
            app_list_json.succeed = "succeed";
            var msg_arr=app_list_json.content.msg;
            for (let i = 0; i < posts.length; i++) {
                msg_arr[i] = posts[i];
            }
            res.send(app_list_json);
        }
    });

});
router.get('/deleteApp', function (req, res) {
    var json= {
        "code": "1",
        "succeed": "succeed",
        "content": {
            "msg": ""
        }
    };
    app_ame = req.param('app_ame');
    mongoosekeeper.use(remove, function (err, posts) {
        if (err) {
            throw err;
        }
        else {
            app_list_json.code = "1";
            app_list_json.succeed = "succeed";
            app_list_json.content.msg="成功删除";
            res.send(app_list_json);
        }
    });

});
//proxy 参数即为mongoosekeeper.use的第二个参数
function queryArticle(proxy) {
    console.log("~~~~~~~~username:"+username)
    if(typeof(username)!="undefined")
    var search = {'username': username};
    else
        var search = {'app_ame': app_ame};
    var query = articleModel.find(search).limit(10);
    query.sort('-create_date');
    query.exec(proxy);
}
function remove(proxy) {
    var search = {'app_ame': app_ame};
    var query = articleModel.remove(search);
    query.exec(proxy);
}

var FailureJson = function (json, res, msg) {
    json.code = "0";
    json.succeed = "failure";
    json.content.msg = msg;
    res.send(json);
};
var SucceeJson = function (json, res, msg) {
    json.code = "1";
    json.succeed = "succeed";
    json.content.msg = msg;
    res.send(json);
};

module.exports = router;